package com.ifnk.portaljava.dapWeb.advance;

import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;
//import SQLite.JDBCDriver;




/**
 * <p>Title:数据库访问类 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author  li.guo.hlyt@gmail.com
 * @version 1.0
 */
public class DBAccess {
	private static String url;
	private static String username;
	private static String password;
    /**
     * 数据库驱动参数属性类
     */
	
    public DBAccess() {
    }

    static {
		Locale locale = Locale.ENGLISH;
		ResourceBundle localResource = ResourceBundle.getBundle("database",locale);
		url = localResource.getString("url");
		username = localResource.getString("username");
		password = localResource.getString("password");
	}
   /**
    * 连接数据库
    * @return Connection: 数据库连接;
    */
   public static Connection getConn() {
       Connection conn = null ; // Our JDBC connection to the database server
       try
        {

// Jdbc Driver
           //Class.forName ("SQLite.JDBCDriver");
    	  // org.sqlite.JDBC
    	   try{
    	  Class.forName("com.mysql.jdbc.Driver");
    	 
    	   }catch(Exception ex)
    	   {
    		 
    		   Class.forName ("com.mysql.jdbc.Driver");   
    	   }
    	   
    	   conn = DriverManager.getConnection(url, username, password);
     
           //System.out.println("Established successful connection");

        }
        catch (Exception ex)
        {
            ex.printStackTrace ();
        }


       return conn;

   }
  
  
   public static void freeConnection(Connection con){
       if(con!=null){
        try {
            con.close();
            con = null;
        }
        catch (SQLException ex) {
            System.out.println("freeConnection Error");
        }

        //System.out.println("freeConnection");
       }
   }

   public static int insertData( String sqlStr ) {
       int result = 0 ;
       Connection cnn = null ;
       Statement stmt = null ;
       try {
           cnn = getConn() ;
           cnn.setAutoCommit(false);
           stmt = cnn.createStatement( ) ;
           stmt.executeUpdate( sqlStr ) ;
           cnn.commit();
       }
       catch ( Exception e ) {
           e.printStackTrace();
           System.out.println(  "Error in DBAccess.insertData: " + e + " sql: " +
                   sqlStr ) ;
             try{
                 cnn.rollback();
             }catch(Exception ee){

             }
           result = -1 ;
       }
       finally {
           freeDB(cnn,stmt);
       }
       return result ;
   }

   /**
    * 查询一个值
    * @param sqlStr sql语句
    * @return String 返回值
    */
   public static String queryAValue( String sqlStr ) {
       Connection cnn = null ;
       Statement stmt = null ;
       ResultSet rs = null ;
       String result=null;

       try {
           cnn = getConn() ;
           stmt = cnn.createStatement() ;
           rs = stmt.executeQuery( sqlStr ) ;
           while ( rs.next() ) {
               result = rs.getString( 1 )  ;
           }
       }
       catch ( Exception e ) {
           System.out.println( "Error in DBAccess.queryAValue: " + e + " sql: " +
                     sqlStr ) ;
           result = "";
       }
       finally {
            freeDB(cnn,stmt,rs);
       }
       return result ;
   }

	public  ResultSet queryTable( String sqlStr ) {
	        Connection cnn = null ;
	        Statement stmt = null ;
	        ResultSet rs = null ;
	     try {
	           cnn  = getConn();
	           stmt = cnn.createStatement( ) ;
	           rs   = stmt.executeQuery(sqlStr) ;
	          
	         
	         
	           
	       }
	       catch ( Exception e ) {
	           System.out.println( "Error in DBAccess.queryAValue: " + e + " sql: " +
	                     sqlStr ) ;
	          
	       }
	       finally {
//	            freeDB(cnn,stmt);
	       }
	       return   rs ;
	   }
	
   public static void freeDB(Connection conn, Statement stat){
       try {
           if ( stat != null ){
                  stat.close() ;
                  stat = null;
              }
          }
          catch ( Exception e ) {
              System.out.println( "Error in DBAccess.freeDB: " + e ) ;
          }finally{
              freeConnection(conn);
          }

   }
   
   

   public static void freeDB(Connection conn, Statement stat, ResultSet rs){
          try {
              if (rs != null){
                  rs.close();
                  rs = null;
              }
                 if ( stat != null ){
                     stat.close() ;
                     stat = null;
                 }

             }
             catch ( Exception e ) {
                 System.out.println( "Error in DBAccess.freeDB: " + e ) ;
             }finally{
                 freeConnection(conn);
             }


      }

    @SuppressWarnings("unused")
	private static void SQLExHandler (SQLException ex){
           // System.out.println ("\n*** SQLException caught ***\n");
            while (ex != null)
            {
//                    System.out.println ("SQLState: " +ex.getSQLState ());
//                    System.out.println ("Message:  " +ex.getMessage ());
//                    System.out.println ("Vendor:   " +ex.getErrorCode ());
//                    System.out.println ("ERROR:   " +ex.toString());
                    ex = ex.getNextException ();
                  //  System.out.println ("");
            }
    }
}


